package dao;

import entity.UserEntity;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import utilities.HibernateUtil;

import java.util.List;

public final class UserDAO {
    public static boolean seekReuseEmail(String emailforseek){
        Session s = null;
        try{
            s = HibernateUtil.getSession();
            Criteria criteria = s.createCriteria(UserEntity.class);

            criteria.add(Restrictions.eq("email",emailforseek));
            List<UserEntity> list = criteria.list();

            if(list.isEmpty())
                return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return true;
    }


    public static boolean createUser(String username,String password,String email){
        Session s = null;
        try{
            s = HibernateUtil.getSession();
            s.beginTransaction();
            UserEntity newUser = new UserEntity();
            newUser.setUsername(username);
            newUser.setPassword(password);
            if(email != null) newUser.setEmail(email);

            s.save(newUser);
            s.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();
            s.getTransaction().rollback();
            return false;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return true;
    }

    public static boolean login(String email, String password){
        Session s = null;
        try{
            s = HibernateUtil.getSession();
            Criteria criteria = s.createCriteria(UserEntity.class);

            criteria.add(Restrictions.eq("email",email));
            criteria.add(Restrictions.eq("password",password));
            List<UserEntity> list = criteria.list();

            if(list.isEmpty())
                return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return true;
    }

    public static int getIDByEmail(String email){
        Session s = null;
        int result = 0;
        try{
            s = HibernateUtil.getSession();
            Criteria criteria = s.createCriteria(UserEntity.class);

            criteria.add(Restrictions.eq("email",email));
            List<UserEntity> list = criteria.list();
            result = list.get(0).getId();
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return result;
    }

    public static String getPasswordByEmail(String email){
        Session s = null;
        String result = null;
        try{
            s = HibernateUtil.getSession();
            Criteria criteria = s.createCriteria(UserEntity.class);

            criteria.add(Restrictions.eq("email",email));
            List<UserEntity> list = criteria.list();
            result = list.get(0).getPassword();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return result;
    }

    public static UserEntity getUserByID(int id){
        Session s = null;
        UserEntity result = null;
        try{
            s = HibernateUtil.getSession();
            Criteria criteria = s.createCriteria(UserEntity.class);

            criteria.add(Restrictions.eq("id",id));
            List<UserEntity> list = criteria.list();
            result = list.get(0);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally{
            HibernateUtil.safeCloseSession(s);
        }

        return result;
    }

}
